<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\parkingService;
use app\service\orderService;
use think\Db;
use think\db\Where;
class Record extends Common{
    public function carin(){
        //查询当前ID用户类型
        $parkingNo = $this->getParkingNo();
        $whe=new Where();
        $com = (new Common())->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);

        //根据账号查询停车场
        $parkinglist = (new orderService())->parkingList($whe);
        $this->assign('parkinglist',$parkinglist);
        $page = request()->has('page') ? request()->get('page') : 1;
        $where=array();
        $data = request()->get();
        if (!empty($data)){
            if(!empty($data['parkingNo'])){
                $where['parkingNo'] = $data['parkingNo'];//停车场名称
            }
            if(!empty($data['carPlateNo'])){
                $where['carPlateNo'] = $data['carPlateNo'];//车牌号码
            }

            $this->assign('where',$where);

        }
        $recordlist = Db::name('car_order_in')->where('parkingNo','in',$parkingNo)->where($where)->order('id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
        $number =  Db::name('car_order_in')->where('parkingNo','in',$parkingNo)->where($where)->count();
        $this->assign("list",$recordlist);
        $this->assign("number",$number);
        foreach ($data as $k => $v) {
            $this->assign($k, $v);

        }
        return $this->fetch();
    }
    //停车记录查询
    public function recordList(){
        //查询当前ID用户类型
        $com = $this->pidTokenId();
        $community_id = implode(',',$com['community_id']);
        //根据当前账号下的所有区域查询所有停车场
        $parkinglist = (new parkingService())->parkingList($community_id);
        $this->assign('parkinglist',$parkinglist);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $out_parking_id[] = $park['out_parking_id'];
            }
            //将数组转换成字符串
            $out_parking_id = implode(',',$out_parking_id);
        }else{
            $out_parking_id = null;
        }
        $data = request()->get();
        if (!empty($data)){
            if(!empty($data['out_parking_id'])){
                $where['out_parking_id'] = $data['out_parking_id'];//停车场名称
            }else{
                $where['out_parking_id'] = $out_parking_id;
            }
            if(!empty($data['car_number'])){
                $where['car_number'] = $data['car_number'];//车牌号码
            }else{
                $where['car_number'] = "";
            }
            if(!empty($data['status'])){
                $where['status'] = $data['status'];//状态
            }else{
                $where['status'] = "";
            }
            $this->assign('where',$where);   
            $where = array_filter($where);
        }else{
            $where=array();
        }
        $export = $where;
        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        $this->assign('export',$export);
        $page = request()->has('page') ? request()->get('page') : 1;
        //根据当前停车场ID查询停车记录
        $recordlist = (new parkingService())->recordWhereList($out_parking_id,$page,$where);
        $number = count((new parkingService())->recordWhereNumber($out_parking_id,$where));
        $this->assign('number',$number);
        if(!empty($number)){
            foreach($recordlist as $lis){
                //根据停车场ID查询停车场名称
                $parkingidinfo = (new parkingService())->parkingIdInfo($lis['out_parking_id']);
                $lis['parking_name'] = $parkingidinfo['parking_name'];
                $lis['community_id'] = (new parkingService())->communityIdName($parkingidinfo['community_id']);
                $list[] = $lis;
            }
            $this->assign('list',$list);
        }else{
            $this->assign('list',$recordlist);
        }
        $this->assign('recordlist',$recordlist);
        foreach ($data as $k => $v) {
            $this->assign($k, $v);
        }
        return $this->fetch('record_list');
    }
	
	//导出车辆进出数据
    public function recordExcel(){
        //获取账单数据
        $export = request()->param('export');
        $export = str_replace("|","\"",$export);
        //查询当前ID用户类型
        $com = $this->pidTokenId();
        $community_id = implode(',',$com['community_id']);
        //根据当前账号下的所有区域查询所有停车场
        $parkinglist = (new parkingService())->parkingList($community_id);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $out_parking_id[] = $park['out_parking_id'];
            }
            //将数组转换成字符串
            $out_parking_id = implode(',',$out_parking_id);
        }else{
            $out_parking_id = null;
        }
        $where = json_decode($export,true);
        if (!empty($where)){
            $recordlist = Db::name('record')->where('out_parking_id','in',$out_parking_id)->where($where)->order('id desc')->select();
            if ($recordlist){
                foreach($recordlist as $lis){
                    //根据停车场ID查询停车场名称
                    $parkingidinfo = (new parkingService())->parkingIdInfo($lis['out_parking_id']);
                    $lis['parking_name'] = $parkingidinfo['parking_name'];
                    $lis['community_id'] = (new parkingService())->communityIdName($parkingidinfo['community_id']);
                    $list[] = $lis;
                }
            }else{
                return json(['info'=>'无数据']);
            }
        }else{
            $recordlist = Db::name('record')->where('out_parking_id','in',$out_parking_id)->order('id desc')->select();
            if($recordlist){
                foreach($recordlist as $lis){
                    //根据停车场ID查询停车场名称
                    $parkingidinfo = (new parkingService())->parkingIdInfo($lis['out_parking_id']);
                    $lis['parking_name'] = $parkingidinfo['parking_name'];
                    $lis['community_id'] = (new parkingService())->communityIdName($parkingidinfo['community_id']);
                    $list[] = $lis;
                }
            }else{
                return json(['info'=>'无数据']);
            }

        }
        //设置表头
        //引入PHPExcel类
        vendor('PHPExcel.PHPExcel.Reader.Excel2007');
        $objPHPExcel = new \PHPExcel();
        //设置表头
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'ISV停车场ID')
            ->setCellValue('B1', '区域名称')
            ->setCellValue('C1', '停车场名称	')
            ->setCellValue('D1', '车牌号码')
            ->setCellValue('E1', '入场时间')
            ->setCellValue('F1', '出场时间')
            ->setCellValue('G1', '停车时长')
            ->setCellValue('H1', '状态');

        $i=2;  //定义一个i变量，目的是在循环输出数据是控制行数
        $count = count($list);  //计算有多少条数据
        for ($i = 2; $i <= $count+1; $i++) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $list[$i-2]["out_parking_id"]);//ISV停车场ID
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $list[$i-2]["community_id"]);//区域名称
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $list[$i-2]["parking_name"]);//停车场名称
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $list[$i-2]["car_number"]);//车牌号码
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $list[$i-2]["in_time"]);//入场时间
            $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $list[$i-2]["out_time"]);//出场时间
            $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $list[$i-2]["parking_time"]);//停车时长
            //判断状态
            if($list[$i-2]["status"] == '1'){
                $status = '停车中';
            }elseif($list[$i-2]["status"] == '2'){
                $status = '已离场';
            }
            $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $status);
        }
        $objPHPExcel->getActiveSheet()->setTitle('车辆进出场信息');      //设置sheet的名称
        $objPHPExcel->setActiveSheetIndex(0);                   //设置sheet的起始位置
        header('Content-Disposition: attachment;filename="车辆进出场信息.xls"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007"); //通过PHPExcel_IOFactory的写函数将上面数据写出来
        $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
    }
	
	//手动开闸记录
    public function openList(){

        //查询当前ID用户类型
        $com = $this->pidTokenId();
        $community_id = implode(',',$com['community_id']);
        //根据当前账号下的所有区域查询所有停车场
        $parkinglist = (new parkingService())->parkingList($community_id);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $out_parking_id[] = $park['out_parking_id'];
            }
            //将数组转换成字符串
            $out_parking_id = implode(',',$out_parking_id);
        }else{
            $out_parking_id = null;
        }
        $data = request()->get();
        //dump($data);die;
        if (!empty($data)){
            if(!empty($data['out_parking_id'])){
                $where['out_parking_id'] = $data['out_parking_id'];//停车场名称
            }else{
                $where['out_parking_id'] = '';
            }
            if(!empty($data['car_number'])){
                if(is_array($data['car_number'])){
                    $where['car_number'] = array('like','%'.$data['car_number'][1].'%');//车牌号码
                }else{
                    $where['car_number'] = array('like','%'.$data['car_number'].'%');//车牌号码
                }
            }else{
                $where['car_number'] = "";
            }
            if(!empty($data['system_name'])){
                if(is_array($data['system_name'])){
                    $where['system_name'] = array('like','%'.$data['system_name'][1].'%');//设备名称
                }else{
                    $where['system_name'] = array('like','%'.$data['system_name'].'%');//设备名称
                }
            }else{
                $where['system_name'] = "";
            }
            if(!empty($data['start_time'])){
                $start_time = $data['start_time'];
                $this->assign('start_time',$start_time);
            }else{
                $start_time = '0000-00-00 00:00:00';
            }
            if(!empty($data['end_time'])){
                $end_time = $data['end_time'];
                $this->assign('end_time',$start_time);
            }else{
                $end_time = '9999-12-31 23:59:59';
            }
            $this->assign('where',$where);
            $where = array_filter($where);
        }else{
            $where=array();
            $start_time = '0000-00-00 00:00:00';
            $end_time = '9999-12-31 23:59:59';
        }
        //dump($where);die;
        $export = $where;
        $export['start_time'] = $start_time;
        $export['end_time'] = $end_time;
        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        $this->assign('export',$export);
        $page = request()->has('page') ? request()->get('page') : 1;

        $openList = (new parkingService())->openWhereList($out_parking_id,$where,$start_time,$end_time,$page);
        $number = Db::name('open_reason')->where('out_parking_id','in',$out_parking_id)->where($where)->where('open_time','>=',$start_time)->where('open_time','<=',$end_time)->count();
        $this->assign('number',$number);
        if($number){
            foreach($openList as $lis){
                //根据停车场ID查询停车场名称
                $parkingidinfo = (new parkingService())->parkingIdInfo($lis['out_parking_id']);
                $lis['parking_name'] = $parkingidinfo['parking_name'];
                $list[] = $lis;
            }
            $this->assign('list',$list);
        }else{
            $this->assign('list',$openList);
        }
        $this->assign('openList',$openList);
        $this->assign('parkinglist',$parkinglist);
        foreach ($data as $k => $v) {
            if(is_array($v)){
                $v=str_replace('%','',$v[1]);
            }
            $this->assign($k, $v);
        }
        //dump($data);die;
        return $this->fetch();
    }

    //导开闸记录数据
    public function openExcel(){
        //获取账单数据
        $export = request()->param('export');
        $export = str_replace("|","\"",$export);
        //查询当前ID用户类型
        $com = $this->pidTokenId();
        $community_id = implode(',',$com['community_id']);
        //根据当前账号下的所有区域查询所有停车场
        $parkinglist = (new parkingService())->parkingList($community_id);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $out_parking_id[] = $park['out_parking_id'];
            }
            //将数组转换成字符串
            $out_parking_id = implode(',',$out_parking_id);
        }else{
            $out_parking_id = null;
        }
        $where = json_decode($export,true);
        $start_time = $where['start_time'];
        $end_time = $where['end_time'];
        unset($where['start_time']);
        unset($where['end_time']);
        $openList = Db::name('open_reason')->where('out_parking_id','in',$out_parking_id)->where('open_time','>=',$start_time)->where('open_time','<=',$end_time)->where($where)->order('id desc')->select();
        if ($openList){
            foreach($openList as $lis){
                //根据停车场ID查询停车场名称
                $parkingidinfo = (new parkingService())->parkingIdInfo($lis['out_parking_id']);
                $lis['parking_name'] = $parkingidinfo['parking_name'];
                $list[] = $lis;
            }
        }else{
            return json(['info'=>'无数据']);
        }
        //设置表头
        //引入PHPExcel类
        vendor('PHPExcel.PHPExcel.Reader.Excel2007');
        $objPHPExcel = new \PHPExcel();
        //设置表头
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '停车场名称')
            ->setCellValue('B1', '车牌号码')
            ->setCellValue('C1', '开闸原因')
            ->setCellValue('D1', '开闸设备')
            ->setCellValue('E1', '开闸设备');
        $i=2;  //定义一个i变量，目的是在循环输出数据是控制行数
        $count = count($list);  //计算有多少条数据
        for ($i = 2; $i <= $count+1; $i++) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $list[$i-2]["parking_name"]);//ISV停车场ID
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $list[$i-2]["car_number"]);//停车场名称
            if($list[$i-2]['reason'] == 1){
                $reason = '无牌车入场';
            }elseif ($list[$i-2]['reason'] == 2){
                $reason = '无金额，未启杆';
            }elseif ($list[$i-2]['reason'] == 3){
                $reason = '业务来往车';
            }elseif ($list[$i-2]['reason'] == 4){
                $reason = '月租车产生费用';
            }elseif ($list[$i-2]['reason'] == 5){
                $reason = '已付款未抬杆';
            }
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $reason);//开闸原因
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $list[$i-2]["system_name"]);//车牌号码
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $list[$i-2]["open_time"]);//入场时间
        }
        $objPHPExcel->getActiveSheet()->setTitle('开闸记录信息');      //设置sheet的名称
        $objPHPExcel->setActiveSheetIndex(0);                   //设置sheet的起始位置
        header('Content-Disposition: attachment;filename="开闸记录信息.xls"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007"); //通过PHPExcel_IOFactory的写函数将上面数据写出来
        $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
    }
	//车辆入场记录
	public function carRecordEntryList(){
		//查询当前ID用户类型
        $com = $this->pidTokenId();
        $community_id = implode(',',$com['community_id']);
        //根据当前账号下的所有区域查询所有停车场
        $parkinglist = (new parkingService())->parkingList($community_id);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $out_parking_id[] = $park['out_parking_id'];
            }
            //将数组转换成字符串
            $out_parking_id = implode(',',$out_parking_id);
        }else{
            $out_parking_id = null;
        }
        $data = request()->get();
		
        //dump($data);die;
        if (!empty($data)){
            if(!empty($data['out_parking_id'])){
                $where['out_parking_id'] = $data['out_parking_id'];//停车场名称
            }else{
                $where['out_parking_id'] = '';
            }
			if(!empty($data['car_type'])){
                $where['car_type'] = $data['car_type'];
            }else{
                $where['car_type'] = '';
            }
            if(!empty($data['car_number'])){
				$this->assign("car_number",$data['car_number']);
				$where['car_number'] = array('like','%'.$data['car_number'].'%');//车牌号码
                /*if(is_array($data['car_number'])){
                    $where['car_number'] = array('like','%'.$data['car_number'][1].'%');//车牌号码
                }else{
                    $where['car_number'] = array('like','%'.$data['car_number'].'%');//车牌号码
                }*/
            }else{
                $where['car_number'] = "";
            }
            $this->assign('where',$where);
            $where = array_filter($where);
        }else{
            $where=array();
        }
		$where["ctype"]="1";
		$page = request()->has('page') ? request()->get('page') : 1;
		$recordlist=Db::table('car_record_entry')->where('out_parking_id','in',$out_parking_id)->where($where)->order('id desc')->paginate(50, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
        $number = count((Db::table('car_record_entry')->where('out_parking_id','in',$out_parking_id)->where($where)->select()));
		$this->assign("recordlist",$recordlist);
		$this->assign("number",$number);
		$this->assign("parkinglist",$parkinglist);
		foreach ($data as $k => $v) {
            if(is_array($v)){
                $v=str_replace('%','',$v[1]);
            }
            $this->assign($k, $v);
        }
		return $this->fetch("car_record_entry_list");
	}
	
	
	//临时车确定入场记录
	public function carTemporaryRecordEntry(){
		//查询当前ID用户类型
        $com = $this->pidTokenId();
        $community_id = implode(',',$com['community_id']);
        //根据当前账号下的所有区域查询所有停车场
        $parkinglist = (new parkingService())->parkingList($community_id);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $out_parking_id[] = $park['out_parking_id'];
            }
            //将数组转换成字符串
            $out_parking_id = implode(',',$out_parking_id);
        }else{
            $out_parking_id = null;
        }
        $data = request()->get();
		
        //dump($data);die;
        if (!empty($data)){
            if(!empty($data['out_parking_id'])){
                $where['out_parking_id'] = $data['out_parking_id'];//停车场名称
            }else{
                $where['out_parking_id'] = '';
            }
            if(!empty($data['car_number'])){
				$this->assign("car_number",$data['car_number']);
				$where['car_number'] = array('like','%'.$data['car_number'].'%');//车牌号码
                /*if(is_array($data['car_number'])){
                    $where['car_number'] = array('like','%'.$data['car_number'][1].'%');//车牌号码
                }else{
                    $where['car_number'] = array('like','%'.$data['car_number'].'%');//车牌号码
                }*/
            }else{
                $where['car_number'] = "";
            }
            $this->assign('where',$where);
            $where = array_filter($where);
        }else{
            $where=array();
        }
		$where["ctype"]="2";
		$page = request()->has('page') ? request()->get('page') : 1;
		$recordlist=Db::table('car_record_entry')->where('out_parking_id','in',$out_parking_id)->where($where)->order('id desc')->paginate(50, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
        $number = count((Db::table('car_record_entry')->where('out_parking_id','in',$out_parking_id)->where($where)->select()));
		$this->assign("recordlist",$recordlist);
		$this->assign("number",$number);
		$this->assign("parkinglist",$parkinglist);
		
		return $this->fetch("car_temporary_record_entry");
	}
	
	
	
	//车辆出场记录
	public function carRecordOutList(){
		//查询当前ID用户类型
        $com = $this->pidTokenId();
        $community_id = implode(',',$com['community_id']);
        //根据当前账号下的所有区域查询所有停车场
        $parkinglist = (new parkingService())->parkingList($community_id);
        //查询停车场ID
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $out_parking_id[] = $park['out_parking_id'];
            }
            //将数组转换成字符串
            $out_parking_id = implode(',',$out_parking_id);
        }else{
            $out_parking_id = null;
        }
        $data = request()->get();
        //dump($data);die;
        if (!empty($data)){
            if(!empty($data['out_parking_id'])){
                $where['out_parking_id'] = $data['out_parking_id'];//停车场名称
            }else{
                $where['out_parking_id'] = '';
            }
			if(!empty($data['car_type'])){
                $where['car_type'] = $data['car_type'];
            }else{
                $where['car_type'] = '';
            }
            if(!empty($data['car_number'])){
				$this->assign("car_number",$data['car_number']);
				$where['car_number'] = array('like','%'.$data['car_number'].'%');//车牌号码
                
            }else{
                $where['car_number'] = "";
            }
            $this->assign('where',$where);
            $where = array_filter($where);
        }else{
            $where=array();
        }
		$page = request()->has('page') ? request()->get('page') : 1;
		$recordlist=Db::table('car_record_out')->where('out_parking_id','in',$out_parking_id)->where($where)->order('id desc')->paginate(50, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
        $number = count((Db::table('car_record_out')->where('out_parking_id','in',$out_parking_id)->where($where)->select()));
		$this->assign("recordlist",$recordlist);
		$this->assign("number",$number);
		$this->assign("parkinglist",$parkinglist);
		foreach ($data as $k => $v) {
            if(is_array($v)){
                $v=str_replace('%','',$v[1]);
            }
            $this->assign($k, $v);
        
        }
		return $this->fetch("car_record_out_list");
	}
	
}